home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / var.c < prev    next >
C/C++ Source or Header  |  1997-12-07  |  5KB  |  236 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/var.c 1.11 1997/06/14 16:35:42 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.4.
  5.  
  6.   Collects the variable definition stuff.
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96,97 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. /*+ Control the output of debugging information from this file. +*/
  17. #define DEBUG 0
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #ifdef AMIGA /* olsen */
  24. #include "amiga.h"
  25. #endif /* AMIGA */
  26.  
  27. #include "memory.h"
  28. #include "datatype.h"
  29. #include "cxref.h"
  30.  
  31. /*+ The file that is currently being documented. +*/
  32. extern File CurFile;
  33.  
  34. /*+ The name of the current file. +*/
  35. extern char* parse_file;
  36.  
  37. /*+ When in a header file make a note of which one for the included variables. +*/
  38. extern int in_header;
  39.  
  40. /*+ A list of the variables found at each level of the scope. +*/
  41. static StringList2 *variable;
  42.  
  43. /*+ The number of levels of scope depth allocated. +*/
  44. static int max_scope=0;
  45.  
  46. /*+ The current scope depth. +*/
  47. static int cur_scope=-1;
  48.  
  49.  
  50. static Variable NewVariableType(char *name,char *type);
  51.  
  52.  
  53. /*++++++++++++++++++++++++++++++++++++++
  54.   Function that is called when a variable definition is seen.
  55.  
  56.   char* name The name of the variable.
  57.  
  58.   char* type The type of the variable.
  59.  
  60.   int scope The scope of variable that has been seen.
  61.   ++++++++++++++++++++++++++++++++++++++*/
  62.  
  63. void SeenVariableDefinition(char* name,char* type,int scope)
  64. {
  65.  Variable var;
  66.  int seen=0;
  67.  
  68. #if DEBUG
  69.  printf("#Var.c# Variable definition for '%s'\n",name);
  70. #endif
  71.  
  72.  for(var=CurFile->variables;var;var=var->next)
  73.     if(!strcmp(var->name,name))
  74.       {
  75.        var->scope|=scope;
  76.        seen=1;
  77.        break;
  78.       }
  79.  
  80.  if(!seen)
  81.    {
  82.     var=NewVariableType(name,type);
  83.  
  84.     var->comment=MallocString(GetCurrentComment());
  85.     var->scope=scope;
  86.  
  87.     if(in_header && !(scope&EXTERN_H))
  88.        var->incfrom=MallocString(parse_file);
  89.  
  90.     AddToLinkedList(CurFile->variables,Variable,var);
  91.    }
  92. }
  93.  
  94. /*++++++++++++++++++++++++++++++++++++++
  95.   Called when a new scope is entered.
  96.   ++++++++++++++++++++++++++++++++++++++*/
  97.  
  98. void UpScope(void)
  99. {
  100.  cur_scope++;
  101.  
  102. #if DEBUG
  103.  printf("#Var.c# Scope ++ (%2d)\n",cur_scope);
  104. #endif
  105.  
  106.  if(cur_scope>=max_scope)
  107.    {
  108.     if(max_scope==0)
  109.        variable=Malloc(16*sizeof(StringList2));
  110.     else
  111.        variable=Realloc(variable,(max_scope+16)*sizeof(StringList2));
  112.     max_scope+=16;
  113.    }
  114.  
  115.  variable[cur_scope]=NewStringList2();
  116. }
  117.  
  118.  
  119. /*++++++++++++++++++++++++++++++++++++++
  120.   Called when an old scope is exited.
  121.   ++++++++++++++++++++++++++++++++++++++*/
  122.  
  123. void DownScope(void)
  124. {
  125. #if DEBUG
  126.  printf("#Var.c# Scope -- (%2d)\n",cur_scope);
  127. #endif
  128.  
  129.  DeleteStringList2(variable[cur_scope]);
  130.  
  131.  cur_scope--;
  132. }
  133.  
  134.  
  135. /*++++++++++++++++++++++++++++++++++++++
  136.   Add a variable to the list of known variables.
  137.  
  138.   char* name The name of the variable.
  139.   ++++++++++++++++++++++++++++++++++++++*/
  140.  
  141. void SeenScopeVariable(char* name)
  142. {
  143. #if DEBUG
  144.  printf("#Var.c# Scope Variable depth %2d '%s'\n",cur_scope,name);
  145. #endif
  146.  
  147.  AddToStringList2(variable[cur_scope],name,NULL,0,0);
  148. }
  149.  
  150.  
  151. /*++++++++++++++++++++++++++++++++++++++
  152.   Check through the scope variables to look for the named one.
  153.  
  154.   int IsAScopeVariable Returns 1 if the name does refer to a variable that is scoped.
  155.  
  156.   char* name The name of the variable to search for.
  157.   ++++++++++++++++++++++++++++++++++++++*/
  158.  
  159. int IsAScopeVariable(char* name)
  160. {
  161.  int i,scope;
  162.  
  163. #if DEBUG
  164.  printf("#Var.c# Lookup variable '%s'\n",name);
  165. #endif
  166.  
  167.  for(scope=cur_scope;scope>=0;scope--)
  168.     for(i=0;i<variable[scope]->n;i++)
  169.        if(!strcmp(variable[scope]->s1[i],name))
  170.           return(1);
  171.  
  172.  return(0);
  173. }
  174.  
  175.  
  176. /*++++++++++++++++++++++++++++++++++++++
  177.   Tidy up all of the local variables in case of a problem and abnormal parser termination.
  178.   ++++++++++++++++++++++++++++++++++++++*/
  179.  
  180. void ResetVariableAnalyser(void)
  181. {
  182.  while(cur_scope>=0)
  183.    {
  184.     DeleteStringList2(variable[cur_scope]);
  185.     cur_scope--;
  186.    }
  187.  
  188.  if(variable) Free(variable);
  189.  variable=NULL;
  190.  
  191.  max_scope=0;
  192.  cur_scope=-1;
  193. }
  194.  
  195.  
  196. /*++++++++++++++++++++++++++++++++++++++
  197.   Create a new variable type.
  198.  
  199.   Variable NewVariableType Returns a new Variable type.
  200.  
  201.   char *name The name of the variable.
  202.  
  203.   char *type The type of the variable.
  204.   ++++++++++++++++++++++++++++++++++++++*/
  205.  
  206. static Variable NewVariableType(char *name,char *type)
  207. {
  208.  Variable var=(Variable)Calloc(1,sizeof(struct _Variable));
  209.  
  210.  var->name   =MallocString(name);
  211.  var->type   =MallocString(type);
  212.  var->visible=NewStringList2();
  213.  var->used   =NewStringList2();
  214.  
  215.  return(var);
  216. }
  217.  
  218.  
  219. /*++++++++++++++++++++++++++++++++++++++
  220.   Delete the specified Variable type.
  221.  
  222.   Variable var The Variable type to be deleted.
  223.   ++++++++++++++++++++++++++++++++++++++*/
  224.  
  225. void DeleteVariableType(Variable var)
  226. {
  227.  if(var->comment) Free(var->comment);
  228.  if(var->name)    Free(var->name);
  229.  if(var->type)    Free(var->type);
  230.  if(var->defined) Free(var->defined);
  231.  if(var->incfrom) Free(var->incfrom);
  232.  if(var->visible) DeleteStringList2(var->visible);
  233.  if(var->used)    DeleteStringList2(var->used);
  234.  Free(var);
  235. }
  236.